<?php

/**
 * 文件上传路径
 * @author Ray
 * @copyright ozchamp.com
 * @version 1.0
 *
 */
class upfile
{
    /**
     * 錯誤信息
     * @var string
     */
    var $errorMsg = '';
    /**
     * 錯誤信息ID
     * @var integer
     */
    var $errorCode = 0;
    /**
     * 上傳文件結果暫存器
     * @var array
     */
    var $ups = array();
    /**
     * 定義上傳文件的目錄,以$_rootpath爲相對路徑
     * @var string
     */
    var $uppath = 'data/upload/';
    var $imgtype = array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF(intel byte order)', 8 => 'TIFF(motorola byte order)', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM', 17 => 'MP4', 18 => '3GP', 19 => 'AVI', 20 => 'MKV', 21 => 'WAV', 22 => 'WMA', 22 => 'OGG', 23 => 'MP3', 24 => 'AAC', 25 => 'MAC', 26 => 'MIDI');

    function __construct()
    {
        if (defined('MOB_UPPATH')) {
            $this->uppath = MOB_UPPATH;
        }
    }

    /**
     * 上傳文件,并返回結果數組
     * @param $n input字段的名字,未空則處理所有的上傳文件
     * @return 結果數組
     */
    function getfile($name = null, $max_size = 0)
    {
        if ($_FILES) {
            $ns = $name ? array($name) : array_keys($_FILES);
            foreach ($ns as $n) {
                if (is_array($_FILES[$n]['name'])) {
                    /*foreach($_FILES[$n]['name'] as $key=>$item) {
                        if($item) {
                            $fileInfo = array('name'=>$_FILES[$n]['name'][$key],'type'=>$_FILES[$n]['type'][$key],'tmp_name'=>$_FILES[$n]['tmp_name'][$key],'error'=>$_FILES[$n]['error'][$key],'size'=>$_FILES[$n]['size'][$key]);
                            $this->ups[$n][]= $this->upfilefun($fileInfo);
                        }
                    }*/
                    /**
                     * @todo 多文件上传
                     */
                } else {
                    //$this->ups[$n] = array('error'=>$_FILES[$n]['size'].'kb'.(int)($max_size),'errid'=>2);
                    //$this->ups[$n] = $this->error(2);
                    $type = strtoupper(substr($_FILES[$n]['name'], (strpos($_FILES[$n]['name'], '.')) + 1));//xjc 获取格式
                    if ($max_size && (int)$_FILES[$n]['size'] > $max_size) {
                        $this->ups[$n] = $this->error(2);
                    } else if (!in_array($type, $this->imgtype)) {//xjc 判断格式
                        $this->ups[$n] = $this->error(10);//
                    } else {
                        $this->ups[$n] = $this->upfilefun($_FILES[$n]);
                    }
                }
            }
            if ($name) {
                return $this->ups[$name];
            } else {
                return $this->ups;
            }
        } else {
            return array();
        }
    }

    /**
     * 创建目录
     * @param string $uppath 目录
     */
    static function mkpath($uppath)
    {
        if (!is_dir($uppath) && $uppath != './' && $uppath != '../') {
            $dirname = '';
            $folders = explode('/', $uppath);
            foreach ($folders as $folder) {
                $dirname .= $folder . '/';
                if ($folder != '' && $folder != '.' && $folder != '..' && !is_dir($dirname)) {
                    @mkdir($dirname);
                    @chmod($dirname, 0777);
                }
            }
            @chmod($uppath, 0777);
        }
    }

    /**
     * 实际上传操作
     * @param string $up_file 单条文件对象
     * @return 返回服务器路径
     */
    function upfilefun($up_file)
    {
        /**
         * 錯誤判斷
         */
        if ($up_file['error']) return $this->error($up_file['error']);
        /**
         * 定位根目錄
         */
        /**
         * 設定上傳目錄并創建
         * @var string
         */
        $uppath = ROOT_PATH2 . $this->uppath . date('Y/m/d/');
        self::mkpath($uppath);
        /**
         * 設定目標文件名
         * @var string
         */
        $upfile = time() . substr($up_file['name'], strrpos($up_file['name'], '.'));
        $i = 0;
        while (file_exists($uppath . $upfile)) {
            $i += 1;
            $upfile = time() . '_' . $i . substr($up_file['name'], strrpos($up_file['name'], '.'));
        }
        /**
         * 文件拷貝
         */
        if (@move_uploaded_file($up_file['tmp_name'], $uppath . $upfile)) {
            $upsucc = true;
        } else if (@copy($up_file['tmp_name'], $uppath . $upfile)) {
            $upsucc = true;
        } else {
            $upsucc = false;
            return array();
        }
        /**
         * 賦值返回數據
         * @var array
         */
        if ($upsucc) {
            $file_data['name'] = $up_file['name'];
            $file_data['size'] = $up_file['size'];
            $file_data['url'] = str_replace(ROOT_PATH2, '', $uppath . $upfile);
            $info = getimagesize($uppath . $upfile);
            if ($info) {
                $file_data['w'] = $info[0];
                $file_data['h'] = $info[1];
                $file_data['type'] = strtolower($this->imgtype[$info[2]]);
            }
            return $file_data;
        } else {
            return $this->error(7);
        }
    }

    public function upFile($file, $path, $path_is_file_name = false)
    {
        if ($file["error"] > 0) {
            $this->error(0);
        }
        if ($path_is_file_name == true) {
            $file_name = $path;
        } else {
            $file_name = $path . time() . '_' . rand(0, 9);
        }

        if (!is_dir(dirname($file_name))) {
            $res = mkdir(dirname($file_name), 0777, true);
        }

        if ($res == false) {
            $this->error(7);
        }

        $res = move_uploaded_file($file["tmp_name"], $file_name);
        if ($res) {
            return true;
        } else {
            $this->error(7);
        }
    }

    /**
     * 返回错误信息
     * @param string $errid 错误值
     * @return 错误信息
     */
    function error($errid)
    {
        switch ($errid) {
            case UPLOAD_ERR_OK :
                $this->errorCode = 0;
                $this->errorMsg = '';
                break;
            case UPLOAD_ERR_INI_SIZE :
                $this->errorMsg = '文件大小超出服務器的限制';
                $this->errorCode = -103;
                break;
            case UPLOAD_ERR_FORM_SIZE :
                $this->errorMsg = '文件大小超出表单的最大尺寸限制';
                $this->errorCode = -103;
                break;
            case UPLOAD_ERR_PARTIAL :
                $this->errorMsg = '文件缺少部分内容';
                $this->errorCode = -101;
                break;
            case UPLOAD_ERR_NO_FILE :
                $this->errorMsg = '没有發現上傳文件';
                $this->errorCode = -102;
                break;
            case UPLOAD_ERR_NO_TMP_DIR :
                $this->errorMsg = '没有找到臨時文件夾';
                $this->errorCode = -102;
                break;
            case UPLOAD_ERR_CANT_WRITE :
                $this->errorMsg = '上傳文件夾權限錯誤';
                $this->errorCode = -102;
                break;
            case 10 :
                $this->errorMsg = '文件格式错误';
                $this->errorCode = -102;
                break;
            default :
                $this->errorMsg = '未知錯誤';
                $this->errorCode = $errid;
                break;
        }
        return array('error' => $this->errorMsg, 'errid' => $this->errorCode);
    }
}

class zipupfile extends upfile
{
    var $imgtype = array(1 => 'ZIP');

    /**
     * 上傳文件,并返回結果數組
     * @param $n input字段的名字,未空則處理所有的上傳文件
     * @return 結果數組
     */
    function getfile($name = null, $max_size = 0)
    {
        if ($_FILES) {
            $ns = $name ? array($name) : array_keys($_FILES);
            foreach ($ns as $n) {
                if (is_array($_FILES[$n]['name'])) {
                    /*foreach($_FILES[$n]['name'] as $key=>$item) {
                        if($item) {
                            $fileInfo = array('name'=>$_FILES[$n]['name'][$key],'type'=>$_FILES[$n]['type'][$key],'tmp_name'=>$_FILES[$n]['tmp_name'][$key],'error'=>$_FILES[$n]['error'][$key],'size'=>$_FILES[$n]['size'][$key]);
                            $this->ups[$n][]= $this->upfilefun($fileInfo);
                        }
                    }*/
                    /**
                     * @todo 多文件上传
                     */
                } else {
                    //$this->ups[$n] = array('error'=>$_FILES[$n]['size'].'kb'.(int)($max_size),'errid'=>2);
                    //$this->ups[$n] = $this->error(2);
                    $type = strtoupper(substr($_FILES[$n]['name'], (strpos($_FILES[$n]['name'], '.')) + 1));//xjc 获取格式
                    if ($max_size && (int)$_FILES[$n]['size'] > $max_size) {
                        $this->ups[$n] = $this->error(2);
                    } else if (!in_array($type, $this->imgtype)) {//xjc 判断格式
                        $this->ups[$n] = $this->error(10);//
                    } else {
                        $this->ups[$n] = $this->upfilefun($_FILES[$n]);
                    }
                }
            }
            if ($name) {
                return $this->ups[$name];
            } else {
                return $this->ups;
            }
        } else {
            return array();
        }
    }

    /**
     * 實際的上傳操作
     * @param $up_file 單條上傳$_FILES記錄
     * @return 返回服務器路徑
     */
    function upfilefun($up_file)
    {
        /**
         * 錯誤判斷
         */
        if ($up_file['error']) return $this->error($up_file['error']);
        /**
         * 定位根目錄
         */
        /**
         * 設定上傳目錄并創建
         * @var string
         */
        $uppath = ROOT_PATH2 . $this->uppath;//.date('Y/m/d/');
        self::mkpath($uppath);
        /**
         * 設定目標文件名
         * @var string
         */
        //$upfile=time().substr($up_file['name'],strrpos($up_file['name'], '.'));
        $upfile = 'cert' . substr($up_file['name'], strrpos($up_file['name'], '.'));
        //$i=0;
        //while(file_exists($uppath.$upfile)){
        //$i+=1;
        //$upfile=time().'_'.$i.substr($up_file['name'],strrpos($up_file['name'], '.'));
        //}
        if (file_exists($uppath . $upfile)) {
            @unlink($uppath . $upfile);
        }
        /**
         * 文件拷貝
         */
        if (@move_uploaded_file($up_file['tmp_name'], $uppath . $upfile)) {
            $upsucc = true;
        } else if (@copy($up_file['tmp_name'], $uppath . $upfile)) {
            $upsucc = true;
        } else {
            $upsucc = false;
            return array();
        }
        /**
         * 賦值返回數據
         * @var array
         */
        if ($upsucc) {
            $file_data['name'] = $up_file['name'];
            $file_data['size'] = $up_file['size'];
            $file_data['url'] = str_replace(ROOT_PATH2, '', $uppath . $upfile);
            $info = getimagesize($uppath . $upfile);
            if ($info) {
                $file_data['w'] = $info[0];
                $file_data['h'] = $info[1];
                $file_data['type'] = strtolower($this->imgtype[$info[2]]);
            }
            return $file_data;
        } else {
            return $this->error(7);
        }
    }

}

?>
